package com.stdspringboot.ctrl;

import com.stdspringboot.service.IUserService;
import com.stdspringboot.util.JWTUtil;
import com.stdspringboot.vo.JsonResult;
import com.stdspringboot.vo.UserInfoJWT;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class LogoffCtrl {

    @Resource
    private IUserService service;

    @RequestMapping("/logoff")
    public JsonResult logoff(HttpServletRequest request) throws IOException {

        String token=request.getHeader("X-Token");
        UserInfoJWT uij= JWTUtil.parse(token, UserInfoJWT.class);

        String userId=uij.getId();

        int i=service.logoff(userId);

        JsonResult jr=null;
        if(i==0){
            jr=JsonResult.fail(JsonResult.CODE_RECORD_NO_EXIST,"该用户不存在，无法注销！");
        }else{
            jr=JsonResult.success("注销成功！");
        }
        return jr;
    }
}
